var t = require("../../../../@babel/runtime/helpers/defineProperty"), e = require("../../../../@babel/runtime/helpers/objectSpread2"), s = getApp();

Component({
    properties: {
        goodsInfo: {
            type: Object,
            value: {}
        },
        categoryMenus: {
            type: Array,
            value: []
        },
        dispatchTemplates: {
            type: Array,
            value: []
        },
        dispatchTypes: {
            type: Array,
            value: []
        },
        goodsId: {
            type: Number,
            value: 0
        }
    },
    data: {
        goodsData: {},
        goodsImg: "",
        otherImgs: [],
        goodsDetail: "",
        goodsName: "",
        goodsCategory: [],
        goodsType: "1",
        needAddress: 0,
        goodsUnit: "",
        price: "",
        marketPrice: "",
        costPrice: "",
        stock: "",
        stockType: 0,
        refund: 0,
        viewMore: !1,
        dispatchData: {
            dispatch_type: 1,
            dispatch_price: "",
            dispatch_id: ""
        },
        distributes: [],
        goods_sn: "",
        product_sn: ""
    },
    lifetimes: {
        attached: function() {
            this.data.goodsData = JSON.parse(JSON.stringify(this.data.goodsInfo)), this.splitGoodsData(this.data.goodsData);
        }
    },
    methods: {
        validatorData: function() {
            if (s._isTextEmpty(this.data.goodsImg)) return s.tips("请选择商品图片");
            if (s._isTextEmpty(this.data.goodsName)) return s.tips("请输入商品名称");
            if (this.data.goodsCategory.length <= 0) return s.tips("请选择商品分类");
            if (s._isTextEmpty(this.data.goodsUnit)) return s.tips("请输入商品单位");
            if (s._isTextEmpty(this.data.price)) return s.tips("请输入商品现价");
            if (s._isTextEmpty(this.data.stock)) return s.tips("请输入商品库存");
            var t = {
                goods_type: this.data.goodsType,
                status: 1,
                title: this.data.goodsName,
                thumb: this.data.goodsImg,
                thumb_url: this.data.otherImgs,
                sku: this.data.goodsUnit,
                content: this.data.goodsDetail,
                market_price: this.data.marketPrice,
                price: this.data.price,
                cost_price: this.data.costPrice,
                stock: this.data.stock,
                reduce_stock_method: this.data.stockType,
                no_refund: this.data.refund,
                need_address: this.data.needAddress,
                category: this.filterCategory(this.data.goodsCategory),
                goods_sn: this.data.goods_sn,
                product_sn: this.data.product_sn
            };
            return t.widgets = {
                dispatch: e(e({}, this.data.dispatchData), {}, {
                    dispatch_type_ids: this.data.distributes
                })
            }, t;
        },
        filterCategory: function(t) {
            var e = [], s = [], i = [];
            return t.forEach(function(t, a) {
                t.parentid && e.push(t.parentid), t.childid && s.push(t.childid), t.thirdid && i.push(t.thirdid);
            }), {
                parentid: e,
                childid: s,
                thirdid: i
            };
        },
        splitGoodsData: function(t) {
            var e = {};
            if (e.goodsImg = t.thumb_src, e.otherImgs = t.thumb_url_src ? t.thumb_url_src : [], 
            e.goodsDetail = t.content, e.goodsName = t.title, e.goodsType = t.type ? t.type : 1, 
            e.needAddress = t.need_address ? t.need_address : 0, e.goodsUnit = t.sku, e.price = t.price, 
            e.marketPrice = t.market_price, e.costPrice = t.cost_price, e.stock = t.stock, e.stockType = t.reduce_stock_method ? t.reduce_stock_method : 0, 
            e.refund = t.no_refund ? t.no_refund : 0, e.goodsCategory = t.has_many_goods_category ? t.has_many_goods_category : [], 
            e.product_sn = t.product_sn, e.goods_sn = t.goods_sn, t.widgets && t.widgets.dispatch) {
                var s = t.widgets.dispatch;
                e.dispatchData = {}, e.dispatchData.dispatch_type = s.dispatch_type, e.dispatchData.dispatch_price = s.dispatch_price, 
                e.dispatchData.dispatch_id = s.dispatch_id, e.distributes = s.dispatch_type_ids;
            }
            this.setData(e);
        },
        viewMoreSet: function() {
            this.setData({
                viewMore: !this.data.viewMore
            });
        },
        updateGoodsImg: function(t) {
            this.data.goodsImg = t.detail[0] ? t.detail[0] : "";
        },
        updateOtherImgs: function(t) {
            this.data.otherImgs = t.detail;
        },
        showGoodsDetailPop: function() {
            this.selectComponent(".goods-details-pop").showPopup();
        },
        saveGoodsDetail: function(t) {
            this.setData({
                goodsDetail: t.detail
            });
        },
        editCategory: function(t) {
            var e = t.detail.manyGoodsCategory, i = this.data.goodsCategory;
            if (i.some(function(t) {
                return e.thirdid ? t.parentid == e.parentid && t.childid == e.childid && t.thirdid == e.thirdid : t.parentid == e.parentid && t.childid == e.childid;
            })) return s.tips("不能重复添加商品分类");
            i.push(e), this.setData({
                goodsCategory: i
            });
        },
        removeCategory: function(t) {
            var e = t.currentTarget.dataset.index, s = this.data.goodsCategory;
            s.splice(e, 1), this.setData({
                goodsCategory: s
            });
        },
        showGoodsTypePop: function() {
            this.selectComponent(".goods-type-pop").showPopup();
        },
        editGoodsType: function(t) {
            this.setData({
                goodsType: t.detail
            });
        },
        switchChange: function(e) {
            var s = e.currentTarget.dataset.name;
            this.setData(t({}, s, e.detail));
        },
        showStockTypePop: function() {
            this.selectComponent(".stock-type-pop").showPopup();
        },
        editStockType: function(t) {
            this.setData({
                stockType: t.detail
            });
        },
        showDispatchTypePop: function() {
            this.selectComponent(".dispatch-type-pop").showPopup();
        },
        editDispatch: function(t) {
            this.setData({
                dispatchData: t.detail
            });
        },
        showDistributeTypePop: function() {
            this.selectComponent(".distribute-type-pop").showPopup();
        },
        editDistribute: function(t) {
            this.setData({
                distributes: t.detail
            });
        }
    }
});